open(BUFF,"SentrixID_LabID.csv") or die "no file exists\n";
$line=<BUFF>;
while(<BUFF>)
{
        chomp($_);
        @array = split(',',$_);
	$array[1] =~ s/\?//g;
	$array[2] =~ s/\?//g;
        $hash{$array[0]} = $array[1];
        $type{$array[0]} = $array[2];
}
open(BUFF,"plate123.log2.fastlonorm.txt") or die "no file exists\n";
$line = <BUFF>;
chomp($line);
$line =~ s/"//g;
@gene_exp_ids = split(/\t/,$line);
shift(@gene_exp_ids);
$tumor=0;
$normal=0;
for($i=0;$i<@gene_exp_ids;$i++)
{
	if(exists($hash{$gene_exp_ids[$i]}))
	{
		#@array = split(
		#print "$gene_exp_ids[$i]\n";	
		@array = split('-',$hash{$gene_exp_ids[$i]});
		$gene_exp_ids{"$array[0]-$array[1]"} = 1;
		#print $hash{$gene_exp_ids[$i]}."\t".$type{$gene_exp_ids[$i]}."\n";	
	}
}
open(BUFF,"BloodID-TisID_new.csv") or die "no file exists\n";
$line=<BUFF>;
while(<BUFF>)
{
	chomp($_);
	@array = split(',',$_);
	$genotype{$array[1]} = $array[2];
}
open(BUFF,"test");
$line=<BUFF>;
chomp($line);
@array = split("\t",$line);
shift(@array);
shift(@array);
shift(@array);
for($i=0;$i<@array;$i++)
{
	$array[$i] =~ s/\.Top Alleles//g;
	
	if(exists($genotype{$array[$i]}) && exists($gene_exp_ids{$genotype{$array[$i]}}))
	{
		#print "$array[$i] $genotype{$array[$i]}\n";
		$final{$genotype{$array[$i]}} =1;
	}
}
#selectting sample and writing them out
open(BUFF,"plate123.log2.fastlonorm.txt") or die "no file exists\n";
open(NORMAL,">normal_expr.txt");
open(TUMOR,">tumor_expr.txt");
$line = <BUFF>;
chomp($line);
$line =~ s/"//g;
@gene_exp_ids = split(/\t/,$line);
#shift(@gene_exp_ids);
@gene_exp_names_tumor = ();
@gene_exp_names_normal = ();
for($i=1;$i<@gene_exp_ids;$i++)
{
        if(exists($hash{$gene_exp_ids[$i]}))
        {
                #@array = split(
                #print "$gene_exp_ids[$i]\n";
                @array = split('-',$hash{$gene_exp_ids[$i]});
		$val = "$array[0]-$array[1]";
                if(exists($final{"$array[0]-$array[1]"})) 
		{
			#print $type{$gene_exp_ids[$i]},"\n";
			chomp($type{$gene_exp_ids[$i]});
			if($type{$gene_exp_ids[$i]} eq "Tumor")
			{
			#print "$array[0]-$array[1]\t$type{$gene_exp_ids[$i]}\n";
				if(!exists($geneexp_processed_tumor{$val}))
				{
					$geneexp_processed_tumor{$val} = $i;
				}
				else
				{
					$geneexp_processed_tumor{$val} =$geneexp_processed_tumor{$val}.','.$i;
				}
			}
			if($type{$gene_exp_ids[$i]} eq "Normal")
                	{
				#print "sucess\n";
                        	#print "$array[0]-$array[1]\t$type{$gene_exp_ids[$i]}\n";
                        	if(!exists($geneexp_processed_normal{$val}))
                        	{
                                	$geneexp_processed_normal{$val} = $i;
                        	}
                        	else
                        	{
                                	$geneexp_processed_normal{$val} =$geneexp_processed_normal{$val}.','.$i;
                        	}
                	}
               } 	#print $hash{$gene_exp_ids[$i]}."\t".$type{$gene_exp_ids[$i]}."\n";
        }
}
print NORMAL "geneID";
foreach $key(sort keys %geneexp_processed_normal)
{
	print NORMAL "\t$key";
}
print NORMAL "\n";
print TUMOR "geneID";
foreach $key(sort keys %geneexp_processed_tumor)
{
        print TUMOR "\t$key";
}
print TUMOR "\n";

#reading the annotation file and storing the start and stop values
open(ANN,"/data2/bsi/RandD/Arraybased_RND/eqtl/file_final") or die "no file found\n";
$line = <ANN>;
chomp($line);
while(<ANN>)
{
	chomp($_);
	@ann = split("\t",$_);
	if($ann[5] ne "null")
	{
	$ann{$ann[0]} = "$ann[0]\t$ann[5]\t$ann[3]\t$ann[4]";
	}
}

#reading platform annotation and converting gene symbol to entrez id
open(ANN,"annotation.txt") or die "no file found\n";
while(<ANN>)
{
        chomp($_);
        @ann = split("\t",$_);
	chomp($ann[0]);
	chomp($ann[10]);
	$plat_ann{$ann[0]} = $ann[10];
	#print $ann[0]."\t".$ann[10]."\n";
}
open(GN_EQTL,">geneloc.txt");
print GN_EQTL "geneid\tchrm_probe\ts1\ts2\n";
while(<BUFF>)
{
	#print "processing $.\n";
	chomp($_);
	$_ =~ s/"//g;
	@gene_exp_ids = split(/\t/,$_);
	chomp($gene_exp_ids[0]);
	$plat_ann{$gene_exp_ids[0]} =~ s/ //g;
	
	if( $plat_ann{$gene_exp_ids[0]} ne "" && exists($plat_ann{$gene_exp_ids[0]}) && exists($ann{$plat_ann{$gene_exp_ids[0]}}) && $ann{$plat_ann{$gene_exp_ids[0]}} !~ m/random/)
	{
		$ann{$plat_ann{$gene_exp_ids[0]}} =~ s/X/23/g;
		$ann{$plat_ann{$gene_exp_ids[0]}} =~ s/Y/24/g;
		print NORMAL $plat_ann{$gene_exp_ids[0]};
        	print TUMOR $plat_ann{$gene_exp_ids[0]};
		print GN_EQTL $ann{$plat_ann{$gene_exp_ids[0]}}."\n";
	#processing normal each row
		foreach $key(sort keys %geneexp_processed_normal)
		{
        		if($geneexp_processed_normal{$key} =~ m/,/)
			{
				@array = ();
				@array = split(',',$geneexp_processed_normal{$key});
				$val = 0;
				for($i =0;$i<@array;$i++)
				{
					$val = $val+$gene_exp_ids[$array[$i]];
				}
				$val = $val/@array;
				print NORMAL "\t$val";
			}
			else
			{
				print NORMAL "\t$gene_exp_ids[$geneexp_processed_normal{$key}]";
			}
		}
		print NORMAL "\n";
		#processing tumor each row
		foreach $key(sort keys %geneexp_processed_tumor)
        	{
                	if($geneexp_processed_tumor{$key} =~ m/,/)
                	{
                        	@array = ();
                        	@array = split(',',$geneexp_processed_tumor{$key});
                        	$val = 0;
                        	for($i =0;$i<@array;$i++)
                        	{
                                	$val = $val+$gene_exp_ids[$array[$i]];
                        	}
                        	$val = $val/@array;
                        	print TUMOR "\t$val";
                	}
               		else
                	{
                        	print TUMOR "\t$gene_exp_ids[$geneexp_processed_tumor{$key}]";
                	}
        	}
        	print TUMOR "\n";
	}
}

#processing the genotype id's

